package com.jeesite.modules.warehouseposition.web;

import java.util.ArrayList;
import java.util.List;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.OrgUtil;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.modules.warehouse.service.WarehouseService;
import com.jeesite.modules.warehousearea.service.WarehouseAreaService;
import com.jeesite.modules.warehouseposition.entity.excel.WarehousePositionExport;
import com.jeesite.modules.warehouseposition.entity.excel.WarehousePositionImport;
import com.jeesite.modules.warehouseposition.entity.qo.WarehousePositionQO;
import com.jeesite.modules.warehouseposition.entity.vo.WarehousePositionVO;
import com.jeesite.modules.warehouseshelves.service.WarehouseShelvesService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.warehouseposition.entity.WarehousePosition;
import com.jeesite.modules.warehouseposition.service.WarehousePositionService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 库位档案Controller
 * @author 码脑洞
 * @version 2025-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouseposition/warehousePosition")
public class WarehousePositionController extends BaseController {

	@Autowired
	private WarehousePositionService warehousePositionService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private WarehouseAreaService warehouseAreaService;
	@Autowired
	private WarehouseShelvesService warehouseShelvesService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WarehousePosition get(Long autoId, boolean isNewRecord) {
		return warehousePositionService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:view")
	@RequestMapping(value = {"list", ""})
	public String list(WarehousePosition warehousePosition, Model model) {
		model.addAttribute("warehousePosition", warehousePosition);
		model.addAttribute("warehouseList", warehouseService.selectWarehouseList());
		model.addAttribute("warehouseAreaList", warehouseAreaService.selectWarehouseAreaList());
		model.addAttribute("warehouseShelvesList", warehouseShelvesService.selectWarehouseShelvesList());
		return "modules/warehouseposition/warehousePositionList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WarehousePositionVO> listData(WarehousePositionQO warehousePositionQO, HttpServletRequest request, HttpServletResponse response) {
		warehousePositionQO.setPage(new Page<>(request, response));
		Page<WarehousePositionVO> page = warehousePositionService.findWarehousePositionPage(warehousePositionQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:view")
	@RequestMapping(value = "form")
	public String form(WarehousePosition warehousePosition, Model model) {
		if (OrgUtil.isValid()) {
			warehousePosition.setOrgId(OrgUtil.getOrgId());
			warehousePosition.setOrgCode(OrgUtil.getOrgCode());
			warehousePosition.setOrgName(OrgUtil.getOrgName());
		}
		model.addAttribute("warehousePosition", warehousePosition);
		model.addAttribute("warehouseList", warehouseService.selectWarehouseList());
		model.addAttribute("warehouseAreaList", warehouseAreaService.selectWarehouseAreaList());
		model.addAttribute("warehouseShelvesList", warehouseShelvesService.selectWarehouseShelvesList());
		return "modules/warehouseposition/warehousePositionForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WarehousePosition warehousePosition) {
		warehousePositionService.save(warehousePosition);
		return renderResult(Global.TRUE, text("保存库位档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(WarehousePosition warehousePosition) {
		warehousePosition.setStatus(WarehousePosition.STATUS_DISABLE);
		warehousePositionService.updateStatus(warehousePosition);
		return renderResult(Global.TRUE, text("停用库位档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(WarehousePosition warehousePosition) {
		warehousePosition.setStatus(WarehousePosition.STATUS_NORMAL);
		warehousePositionService.updateStatus(warehousePosition);
		return renderResult(Global.TRUE, text("启用库位档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("warehouseposition:warehousePosition:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WarehousePosition warehousePosition) {
		warehousePositionService.delete(warehousePosition);
		return renderResult(Global.TRUE, text("删除库位档案成功！"));
	}

	@RequiresPermissions("warehouseposition:warehousePosition:edit")
	@RequestMapping(value = "checkCodeUnique")
	@ResponseBody
	public Boolean checkCodeUnique(String oldCode, String code) {
		return warehousePositionService.checkCodeUnique(oldCode, code);
	}

	@RequiresPermissions("warehouseposition:warehousePosition:view")
	@RequestMapping("exportData")
	public void exportData(WarehousePosition warehousePosition, HttpServletResponse response) {
		List<WarehousePosition> list = warehousePositionService.findList(warehousePosition);
		String fileName = "库位档案数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("库位档案", WarehousePositionExport.class)) {
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * 下载导入数据模板
	 */
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		String fileName = "库位档案-导入模板.xlsx";
		try (ExcelExport ee = new ExcelExport("库位档案", WarehousePositionImport.class, ExcelField.Type.IMPORT)) {
			ee.setDataList(new ArrayList<>()).write(response, fileName);
		}
	}

	/**
	 * 导入数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file) throws Exception {
		return warehousePositionService.importData(file);
	}
}